package umlTool;

import java.util.Vector;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * represents a method in a class
 * 
 * @author Mimo
 * 
 */
public class ClassMethod extends ClassMember implements ISaveable {
	Boolean isAbstract;
	Vector<MethodParameter> parameters;
	private String implementation;

	ClassMethod(Element elem) {
		super(elem);
		parameters = new Vector<MethodParameter>();
		isAbstract = Boolean.parseBoolean(elem.getAttribute("abstract"));
		implementation = elem.getTextContent();
	}

	public ClassMethod() {
		setImplementation("");
		parameters = new Vector<MethodParameter>();
		Static = Final = false;
		isAbstract = false;
	}

	public ClassMethod(String name) {
		this();
		this.name = name;
	}

	public String classType() {
		return "classMember";
	}

	@Override
	public Element saveToXml(Document doc) {
		Element elem = super.saveToXml(doc);
		elem.setAttribute("abstract", isAbstract.toString());
		elem.setTextContent(getImplementation());
		return elem;
	}

	public void addParameters(MethodParameter param) {
		parameters.add(param);
	}

	public String toString() {
		StringBuilder s = new StringBuilder();
		s.append(access.toString() + " ");
		if (isAbstract)
			s.append("abstract ");
		s.append(type + " ");
		s.append(name + "(");
		for (MethodParameter param : parameters) {
			s.append(param.getParamType() + " " + param.getName() + ",");
		}
		if (s.lastIndexOf(",") != -1)
			s.setCharAt(s.lastIndexOf(","), ')');
		else
			s.append(')');
		s.append("{\n");
		s.append(getImplementation() + "\n}\n");
		return s.toString();
	}

	void setImplementation(String implementation) {
		this.implementation = implementation;
	}

	String getImplementation() {
		return implementation;
	}

	@Override
	public void setComment(String comment) {
		this.comment = comment;
	}

	@Override
	public String getComment() {
		return comment;
	}

}
